package model01;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileWriterEx {
	public static void main(String[] args) {
		String str = "";
		Scanner s = new Scanner(System.in);
		
		System.out.print("Nhap chuoi can ghi: ");
		str = s.nextLine();
		
		char buffer[] = new char[str.length()];
		str.getChars(0, str.length(), buffer, 0);
		
		try {
			FileWriter fw = new FileWriter("fw1.txt");
			//ghi lan luot va ghi tat ca du lieu nhap tu ban phim vao file fw1.txt
			//Chu y: neu fw1.txt chua co thi se tao moi
			for(int i = 0; i < str.length(); i++) {
				fw.write(buffer[i]);
			}
			fw.close();
			
			//ghi tat ca du lieu va file fw2.txt, ghi mot lan mot
			FileWriter fw2 = new FileWriter("fw2.txt");
			fw2.write(buffer);
			fw2.close();
			
			//ghi chuoi vao buffer, bat dau tu vi tri thu 2 den vi tri thu 9
			FileWriter fw3 = new FileWriter("fw3.txt");
			fw3.write(buffer, 2, 9);
			fw3.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
